from app.models.index_model import Sample, AiIndex,AddDocumentsJob
from config import ALIYUN_ACCESS_KEY_ID, ALIYUN_ACCESS_KEY_SECRET,WORKSPACE_ID
from sqlalchemy.orm import Session
from app.database.db_setup import get_db


def add_index(
    name:str,
    category_id: str ,
    SinkType: str='BUILT_IN',
    StructureType:str='unstructured',
    SourceType:str='DATA_CENTER_CATEGORY',
):
    try:
        if not ALIYUN_ACCESS_KEY_ID or not ALIYUN_ACCESS_KEY_SECRET or not WORKSPACE_ID:
            return {"error": "请设置 ALIYUN_ACCESS_KEY_ID 和 ALIYUN_ACCESS_KEY_SECRET和WORKSPACE_ID 环境变量"}
        result = Sample.call_api(
            ALIYUN_ACCESS_KEY_ID,
            ALIYUN_ACCESS_KEY_SECRET,
            WORKSPACE_ID,
            category_id,
            name,
            StructureType,
            SourceType,
            SinkType
        )
        return result
    except Exception as e:
        return {"error": str(e)}
async def add_documents(
    IndexId:str,
    category_id: str ,
):
    try:
        if not ALIYUN_ACCESS_KEY_ID or not ALIYUN_ACCESS_KEY_SECRET or not WORKSPACE_ID:
            return {"error": "请设置 ALIYUN_ACCESS_KEY_ID 和 ALIYUN_ACCESS_KEY_SECRET和WORKSPACE_ID 环境变量"}
        result = AddDocumentsJob.call_api(
            ALIYUN_ACCESS_KEY_ID,
            ALIYUN_ACCESS_KEY_SECRET,
            WORKSPACE_ID,
            IndexId,
            category_id,
        )
        return result
    except Exception as e:
        return {"error": str(e)}

def create_index(
    CategoryId:str,
    script_id:int,
    name: str,
    index_id: str,
    add_time: int,
    db: Session
):
    new_index = AiIndex(
        CategoryId=CategoryId,
        script_id=script_id,
        name=name,
        index_id=index_id,
        add_time=add_time
    )
    try:
        db.add(new_index)
        db.commit()
        db.refresh(new_index)
        return new_index
    except Exception as e:
        db.rollback()
        raise e
async def get_ai_index_list(
    db: Session,
):
    query = db.query(AiIndex)
    query = query.filter(AiIndex.CategoryId == '')
    query = query.filter(AiIndex.index_id == '')
    try:
        index_list = query.all()
        result = []
        for index in index_list:
            file_dict = {
                'id': index.id,
                'name': index.name,
                'CategoryId': index.CategoryId,
                'index_id': index.index_id,
            }
            result.append(file_dict)
        return result
    except Exception as e:
        return {"error": str(e)}
def update_ai_index(
    db: Session,
    id: int,
    CategoryId: str = None,
    index_id: str = None,
):
    file_record = db.query(AiIndex).filter(AiIndex.id == id).first()
    if not file_record:
        return {"error": f"未找到 ID 为 {id} 的记录"}
    if CategoryId is not None:
        file_record.CategoryId = CategoryId
    if index_id is not None:
        file_record.index_id = index_id
    try:
        db.commit()
        db.refresh(file_record)
        return file_record
    except Exception as e:
        db.rollback()
        return {"error": str(e)}
